package edu.hawaii.duedates.libraries;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;

import edu.hawaii.duedates.Library;

/**
 * This contains the classes for accessing the Hawaii's State Library.
 * 
 * @author Anthony Du, Vincent Leung
 */
public class HSLLibrary extends Library {
  //
  //  private static HSLLibrary _instance = new HSLLibrary();
  //
  //  /**
  //   * Constructor for Singleton.
  //   */
  //  private HSLLibrary() {
  //    //construct object
  //  }
  //
  //  /**
  //   * Part of the Singleton.
  //   * @return UHMLibrary instance.
  //   */
  //  public static HSLLibrary getInstance() {
  //    return _instance;
  //  }
  
  /**
   * Constructor for creating a library and setting user's credentials.
   * @param n String of library's name.
   * @param id String of user's id.
   * @param key String of user's key.
   */
  public HSLLibrary(String n, String id, String key) {
    super(n, id, key);
  }

  /**
   * Attempts to login to the Hawaii State Library System.
   * @param id The hawaii library id barcode.
   * @param key The key (last four digits of your phone number).
   * @return The WebConversation after logging in. 
   * @throws Exception If errors occur while logging in. 
   */
  @Override
  public WebConversation logIntoLibrary(String id, String key) throws Exception {
    try {
      String url = "http://www.librarieshawaii.org/";
      // Website has bogus JavaScript code.
      HttpUnitOptions.setExceptionsThrownOnScriptError(false);
      // create the conversation object which will maintain state for us
      WebConversation conversation = new WebConversation();
      WebRequest request = new GetMethodWebRequest(url);
      WebResponse response = conversation.getResponse(request);
      WebLink link = response.getLinkWith("My Account");
      link.click();
      response = conversation.getCurrentPage();
      WebForm form = response.getForms()[0];
      form.setParameter("sec1", id);
      form.setParameter("sec2", key);
      form.submit();
      link = response.getLinkWith("Checked Out");
      link.click();
      response = conversation.getCurrentPage();
      WebTable table = response.getTables()[2];
      String s = table.getCellAsText(0, 0);
      System.out.println(s);
      String delims = "[|]+";
      String [] token = s.trim().split(delims);
      token[3] = token[3].replaceAll("\\n", "");
      System.out.println(token[3] + "\t" + token[1]); 
      System.out.print(token[8] + "\t" + token[5]);

      return conversation;
    }
    catch (Exception e) {
      throw new Exception ("Error while logging in to Hawaii State Library", e);
    }
  }

  /**
   * Gets info from the Hawaii State Library site.
   * @return A string providing information about checked out books. 
   * @throws Exception If problems occur getting the information. 
   */
  @Override
  public String getLibInfo() throws Exception {
    try {
      WebConversation conversation = this.logIntoLibrary(getId(), getKey());
      WebResponse response = conversation.getCurrentPage();
      WebLink link = response.getLinkWith("Items Out");
      link.click();
      response = conversation.getCurrentPage();
      boolean noBooksOut = response.getText().contains("Checked Out: 0");
      return (noBooksOut) ? "No items checked out" : "At least one item checked out.";
    }
    catch (Exception e) {
      throw new Exception("Error getting Hawaii State Library info", e);
    }
  }
}